package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.service.*;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.HashMap;

@RestController
@Slf4j
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private StatisticsInfoService statisticsInfoService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LeaveWordService leaveWordService;

    @Autowired
    private TagService tagService;



    /**
     * 首页分页查询
     **/
    @PostMapping({"/myArticles"})
    public String myArticles(int rows, int pageNum) {
        try {
            DataMap dataMap = articleService.selectMyArticleWithPage(rows, pageNum);

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("myArticles error");
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 网站信息
     */
    @GetMapping("/getSiteInfo")
    public String getSiteInfo() {
        try {
            DataMap dataMap = statisticsInfoService.getSiteInfo();

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("getSiteInfo error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 最新评论
     */
    @GetMapping("/newComment")
    public String newComment(Integer rows, Integer pageNum, HttpServletRequest request) {
        try {
            DataMap dataMap = commentService.getNewComment(rows, pageNum);

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("newComment error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 获取最新留言
     * /newLeaveWord
     */
    @GetMapping("/newLeaveWord")
    public String newLeaveWord(Integer rows, Integer pageNum, HttpServletRequest request) {
        try {
            DataMap dataMap = leaveWordService.newLeaveWord(rows, pageNum);

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("newLeaveWord error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * findTagsCloud
     * 获取标签云
     */
    @GetMapping("/findTagsCloud")
    public String findTagsCloud(HttpServletRequest request) {
        try {
            DataMap dataMap = tagService.findTagsCloud();

            return JsonResult.build(dataMap).toJSON();

        } catch (Exception e) {
            log.error("findTagsCloud error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     * 获取最新消息
     *
     */
    @PostMapping("/getUserNews")
    public String getUserNews(HttpServletRequest request , @AuthenticationPrincipal Principal principal) {
        try {
            Principal userPrincipal = request.getUserPrincipal();
            if (userPrincipal != null) {
                String  username = userPrincipal.getName();
                DataMap dataMap = statisticsInfoService.getUserNews(username);

                return JsonResult.build(dataMap).toJSON();
            }else {
                HashMap<String, Object> map = new HashMap<>();
                map.put("result", 0);
                return JsonResult.build(DataMap.success().setData(map)).toJSON();
            }


        } catch (Exception e) {
            log.error("getUserNews error" + e.getMessage());
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }


}
